package com.riki.riki_push_example.jpush;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;


import com.riki.riki_push_example.MainActivity;
import com.riki.riki_push_example.utils.UiUtils;

import java.util.List;

import io.flutter.plugin.common.MethodChannel;


public class JPushOppoActivity extends Activity {


    private static final String TAG = "OpenClickActivity";
    /**
     * 消息Id
     **/
    private static final String KEY_MSGID = "msg_id";
    /**
     * 该通知的下发通道
     **/
    private static final String KEY_WHICH_PUSH_SDK = "rom_type";
    /**
     * 通知标题
     **/
    private static final String KEY_TITLE = "n_title";
    /**
     * 通知内容
     **/
    private static final String KEY_CONTENT = "n_content";
    /**
     * 通知附加字段
     **/
    private static final String KEY_EXTRAS = "n_extras";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && UiUtils.isTranslucentOrFloating(this)) {
            UiUtils.fixOrientation(this);
        }
        handleOpenClick();
    }


    private void handleOpenClick() {
        String data = null;
        //获取华为平台附带的jpush信息
        if (getIntent().getData() != null) {
            data = getIntent().getData().toString();
        }

        //获取fcm、oppo、小米、vivo平台附带的jpush信息
        if (TextUtils.isEmpty(data) && getIntent().getExtras() != null) {
            data = getIntent().getExtras().getString("JMessageExtra");
        }
        if (!isAppRunning()) {
            //说明没有登录
            //说明没有登录
            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra("JMessageExtra", data);
            this.startActivity(intent);
            this.finish();
        } else {
            jpushData(data);
            this.finish();
        }

    }


    public void jpushData(String data) {
//        MyApplication myApplication = MyApplication.getApplication();
//        if (myApplication != null) {
//            MethodChannel mMethodChannel = myApplication.getmMethodChannel();
//            if (mMethodChannel != null) {
//                mMethodChannel.invokeMethod("androidJpushMethod", data);
//            }
//        }
    }


    private boolean isAppRunning() {
        boolean isAppRunning = false;
        ActivityManager am = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1000);
        for (ActivityManager.RunningTaskInfo info : list) {
            ///< 判断HomeActivity是否活着
            if (MainActivity.class.getName().equals(info.baseActivity.getClassName())) {
                isAppRunning = true;
                // find it, break
                break;
            }
        }
        return isAppRunning;
    }
}
